package com.usian.login.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.pojo.ResultVo;
import com.usian.login.dao.LoginDao;
import com.usian.login.dao.UserDao;
import com.usian.login.pojo.TbUser;
import com.usian.login.service.LoginService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserDao userDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public ResultVo sendCode(String phone) {
        //1.首先先判断是否为一个真正的手机号
        //使用了一个hutool工具类
        Boolean isPhone = Validator.isMobile(phone);
        if (isPhone) {
            //2、判断手机号是否已经注册
            int count = loginDao.getUserCountByPhoe(phone);
            if (count > 0) {


//                发送验证码

                rabbitTemplate.convertAndSend("java-exchange", "java-queue", phone);


                return new ResultVo(200, "短信发送成功", null);
            } else {
                return new ResultVo(303, "此手机好不存在或尚未激活", null);
            }

        } else {
            return new ResultVo(303, "手机号错误格式", null);
        }

    }

    /**
     * 登录
     * @param phone 手机号
     * @param code 验证码
     * @return
     */
    @Override
    public ResultVo userLogin(String phone, String code) {
        //  判断手机号  验证码是否填写
        Boolean isHavephone = StrUtil.isEmpty(phone);
        //判断验证码
        Boolean isHavecode = StringUtils.isEmpty(code);

        if (!isHavephone){
            if (!isHavecode){
                //   判断用户是否存在
//                int count = loginDao.getUserCountByPhoe(phone);

                //            返回用户的个人信息
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();

                lambdaQueryWrapper.eq(TbUser::getMobile,phone);
                TbUser tbUser = userDao.selectOne(lambdaQueryWrapper);

                if (tbUser!=null){
//        验证码是否正确  匹配用户输入的和我们保存的是否一致

                    String redisCode = stringRedisTemplate.opsForValue().get(phone);
//             如果验证码过期

                    if (!StrUtil.isEmpty(redisCode)){

                        if (redisCode.equals(code)){
//     直接返回用户信息
                            return new ResultVo(200,"success",tbUser);

                        }else {
                            return new ResultVo(303, "验证码输入错误", null);
                        }

                    }else {
                        return new ResultVo(303, "验证码已超时", null);
                    }

                }else {
                    return new ResultVo(303, "手机号不存在或尚未激活", null);
                }
            }else {
                return new ResultVo(303, "验证码必填", null);
            }
        }else {
            return new ResultVo(303, "手机号必填", null);

        }


    }

    @Override
    public ResultVo save(TbUser tbUser) {
        userDao.insert(tbUser);

        return new ResultVo(200, "success", null);

    }


}
